package com.our.sys.project1.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.set.service.HzService;
import com.our.sys.project1.dao.ProduceDao;
import com.our.sys.project1.entity.Produce;

@Service
@Transactional
public class ProduceService {
    @Autowired
    private ProduceDao produceDao;

    @Autowired
    private HzService hzService;

    private static Logger logger = LoggerFactory.getLogger(ProduceService.class);

    @Transactional(readOnly = true)
    public Page<Produce> query(Page<Produce> page, List<PropertyFilter> filters) {
        Page<Produce> curPage = produceDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public Produce queryById(Long id) {
        return produceDao.findOneBy("id", id);
    }

    @Transactional(readOnly = true)
    private Long queryCountByName(String name) {
        Long result = produceDao.findUnique("select count(a) from Produce a where a.name = ?", name);
        if (null == result) {
            return 0L;
        } else {
            return result;
        }
    }

    public void save(Produce produce) throws Exception {
        try {
            Map<String, String> map = hzService.getMemo(produce.getName());
            produce.setPy(map.get("py"));
            produce.setWb(map.get("wb"));
            if (null != produce.getId()) {
                Produce getProduce = this.queryById(produce.getId());

                if (queryCountByName(getProduce.getName()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个生产商！");
                }

                getProduce.setName(produce.getName());
                getProduce.setPy(produce.getPy());
                getProduce.setWb(produce.getWb());
                getProduce.setAddress(produce.getAddress());
                getProduce.setCountry(produce.getCountry());
                getProduce.setPerson(produce.getPerson());
                getProduce.setShortName(produce.getShortName());
                getProduce.setTel(produce.getTel());
                produceDao.save(getProduce);
            } else {

                if (queryCountByName(produce.getName()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个生产商！");
                }

                produceDao.save(produce);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void delete(Long id) throws Exception {
        try {
            produceDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }
}
